package GUI;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

import MessagePacks.GetAllPack;
import MessagePacks.MessagePack;
import MessagePacks.OneProcessPack;
import MessagePacks.ProcessesPack;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JComboBox;

import entities.CProcess;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class ProcessSelectionScreen  extends MasterGUI{
	
	private static final long serialVersionUID = 1L;
	private MainGUI mainGui;
	private JButton btnSend;
	private JPanel panel;
	private ProcessesPack prc;
	private JComboBox processBox;
	private JComboBox roleBox;
	private boolean prcListenerFlag = false;
	private boolean roleListenerlag = false;
	private SupervisorMainScreen supScreen;
	private ChairmanScreen chScreen;
	private TesterScreen testScreen;
	private EstimatorMenuScreen estimatorScreen;
	private ExecutionLeaderScreen execLdrScreen;
	private OneProcessPack pck;
	private int prcID;
	private GetAllPack gtRlcPack;
	
	public ProcessSelectionScreen(MainGUI maingui) {
		
		super(maingui);
		this.mainGui=maingui;
		

		getProccessSelect();
		
		
		JLabel lblWelcomeToIcm = new JLabel("Process selection");
		lblWelcomeToIcm.setBounds(0, 0, 884, 42);
		lblWelcomeToIcm.setFont(new Font("Trajan Pro", Font.BOLD, 16));
		lblWelcomeToIcm.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcomeToIcm.setAlignmentX(Component.CENTER_ALIGNMENT);
		getContentPane().add(lblWelcomeToIcm);
		
		panel = new JPanel();
		panel.setBorder(null);
		panel.setBounds(267, 96, 392, 289);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		setProcessBox();
		setRoleBox();
		
		
		Iterator<Entry<String, String>> iter = prc.nMap.entrySet().iterator();
		while (iter.hasNext()) {
			
			Map.Entry mEntry = (Map.Entry) iter.next();
			processBox.addItem((String) mEntry.getKey());
			roleBox.addItem((String) mEntry.getValue());
		}
		
		
		setBtnSend();
		
		JLabel lblProcessId = new JLabel("Process ID");
		lblProcessId.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblProcessId.setHorizontalAlignment(SwingConstants.CENTER);
		lblProcessId.setBounds(0, 29, 103, 20);
		panel.add(lblProcessId);
		
		JLabel lblRole = new JLabel("Role");
		lblRole.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblRole.setHorizontalAlignment(SwingConstants.CENTER);
		lblRole.setBounds(240, 29, 103, 20);
		panel.add(lblRole);
		

		

		

	}
	
	
	public JButton getBtnSend() {
		return btnSend;
	}


	public void setBtnSend() {
		btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String selectItem = (String) roleBox.getSelectedItem();
				prcID= Integer.parseInt((String) processBox.getSelectedItem());
				setPck(prcID);
				MainGUI.process=new CProcess(pck.getProcessID(),pck.getInitiatorID() ,pck.getSupervisorID() ,pck.getEstimatorID() ,pck.getChairmanID() ,pck.getExecLeaderID() ,pck.getTesterID() ,pck.getStatus(),pck.getCurrentPhase(),pck.getResponsableOK() );
				if(selectItem.equals("Supervisor")){
					if(pck.getStatus().equals("Suspended")){
						JOptionPane.showMessageDialog(serverGUI.mainframe,"Process freezed.");
					}
					else if(pck.getStatus().equals("Terminated"))
					{
						JOptionPane.showMessageDialog(serverGUI.mainframe,"Process terminated, message sent to initiator.");
					}
					else
					{
						supScreen = new SupervisorMainScreen(mainGui);
						supScreen.setVisible(true);
						close();
					}
				}
				if(selectItem.equals("Estimator")){
					if(!pck.getStatus().equals("Active")){
						JOptionPane.showMessageDialog(serverGUI.mainframe,"Process not Active.");
						}
					else{
						estimatorScreen= new EstimatorMenuScreen(mainGui);
						estimatorScreen.setVisible(true);
						close();
					}
				}
				if(selectItem.equals("chairman")){
					if(!pck.getStatus().equals("Active")){
						JOptionPane.showMessageDialog(serverGUI.mainframe,"Process not Active.");
						}
					else{
						
						chScreen= new ChairmanScreen(mainGui);
						chScreen.setVisible(true);
						close();
						}
				}
				if(selectItem.equals("Execution Leader")){
					if(!pck.getStatus().equals("Active") && !pck.getStatus().equals("waiting for approval")){
						JOptionPane.showMessageDialog(serverGUI.mainframe,"Process not Active.");
						}
					else{
						execLdrScreen= new ExecutionLeaderScreen(mainGui);
						execLdrScreen.setVisible(true);
						close();
						}
					}
				
				
				if(selectItem.equals("Tester")){
					if(!pck.getStatus().equals("Active")){
						JOptionPane.showMessageDialog(serverGUI.mainframe,"Process not Active.");
						}
					else{
						testScreen= new TesterScreen(mainGui);
						testScreen.setVisible(true);
						close();
						}
				}
				
					

			}
		});
		btnSend.setBounds(133, 143, 89, 23);
		panel.add(btnSend);
	}
	
	/**

	 */
	public void getProccessSelect(){
		prc= new ProcessesPack (mainGui.usr);
		mainGui.client.handleMessageFromClientUI(prc);
		prc= (ProcessesPack) mainGui.client.getMessage();
		
	}
	
	



	public JComboBox getProcessBox() {
		return processBox;
	}

	public void setProcessBox() {
		processBox = new JComboBox();
		processBox.addItem("");
		processBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(roleListenerlag == false){
					prcListenerFlag = true;
					roleBox.removeAllItems();
					roleBox.addItem("");
					getAllRolesforProc();
					Iterator<String> iter = gtRlcPack.nArray.iterator();
					while (iter.hasNext()){
						String procId = iter.next();
						roleBox.addItem(procId);
					}
				}
			}
		});
		processBox.setBounds(10, 60, 103, 20);
		processBox.setVisible(true);
		panel.add(processBox);
	}

	public JComboBox getRoleBox() {
		return roleBox;
	}

	public void setRoleBox() {
		roleBox = new JComboBox();
		roleBox.addItem("");
		roleBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(prcListenerFlag == false){
					roleListenerlag = true;
					Iterator<Entry<String, String>> iter = prc.nMap.entrySet().iterator();
					processBox.removeAllItems();
					while (iter.hasNext()) {
						Map.Entry mEntry = (Map.Entry) iter.next();
						if(mEntry.getValue()==roleBox.getSelectedItem()){
							processBox.addItem((String) mEntry.getKey());}
					}
				}
			}
		});
		roleBox.setBounds(240, 60, 103, 20);
		roleBox.setVisible(true);
		panel.add(roleBox);
	}

	
	
	public OneProcessPack getPck() {
		return pck;
	}


	/**
	 * 
	 * @param prcID2 - Process'a ID
	 */

	public void setPck(int prcID2) {
		pck= new OneProcessPack(prcID2);
		mainGui.client.handleMessageFromClientUI(pck);
		pck= (OneProcessPack) mainGui.client.getMessage();
	}
	
	
	
	private void getAllRolesforProc(){
		gtRlcPack = new GetAllPack(MessagePack.getAllRoles);
		gtRlcPack.setInitID(Integer.parseInt((String) processBox.getSelectedItem()));
		gtRlcPack.setNumber(mainGui.usr.GetUser());
		mainGui.client.handleMessageFromClientUI(gtRlcPack);
		gtRlcPack= (GetAllPack) mainGui.client.getMessage();
		
	}

}
